package com.imooc.sso.controller;

import com.imooc.sso.server.AuthorizationServerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Collection;

/**
 * Created by xiaojie.Ma on 2018/6/14.
 */
@Controller
public class LogoutController {

//    /**
//     * 用户注销
//     * @param accessToken
//     * @return
//     */
//    @RequestMapping(value = "/user/logout", method = RequestMethod.POST)
//    public ResponseVO userLogout(@RequestHeader(value = "accessToken", required = true) String accessToken,
//                                 @RequestHeader(value = "userId", required = true) Long userId) throws Exception{
//        OauthAccessToken oauthAccessToken = userMgrService.getOauthAccessToken(accessToken);
//        if(null == oauthAccessToken){
//            return UserResponseCode.buildEnumResponseVO(UserResponseCode.RESPONSE_CODE_OAUTH_ACCESSTOKEN_EMPTY, null);
//        }
//        //刪除OauthToken记录
//        boolean result = userMgrService.revokeOauthToken(oauthAccessToken);
//        if(result){
//            return UserResponseCode.buildEnumResponseVO(UserResponseCode.RESPONSE_RETURN_CODE_SUCCESS, null);
//        }
//        return UserResponseCode.buildEnumResponseVO(UserResponseCode.RESPONSE_CODE_SYSTEM_ERROR, null);
//    }
    @Autowired
    private AuthorizationServerConfig authorizationServerConfig;
    @GetMapping("/test")
    public String test() {
        System.out.println("coming logout-----");
        TokenStore tokenStore = authorizationServerConfig.jwtTokenStore();
        Collection<OAuth2AccessToken> oAuth2AccessTokens = tokenStore.findTokensByClientIdAndUserName("imooc","user");
        for(OAuth2AccessToken oAuth2AccessToken : oAuth2AccessTokens) {
            System.out.println("1");
            System.out.println(oAuth2AccessToken.getTokenType());
            System.out.println("2");
            System.out.println(oAuth2AccessToken.getValue());
            System.out.println("3");
            System.out.println(oAuth2AccessToken.isExpired());
        }
        System.out.println();

        return "";
    }

}
